GameSpace = {
    Width: 0,
    Height: 0,
    Handle: null,
    ArrBitBlock: null,
    ArriveBlock: null,
    BitEmpty: 0x00,
    BitFull: 0xFFFFF,
    Backbround: null,
    GridColor: null,
    HasGrid: false,
    BlockColor: null,
    BlockFore: null,
    SquareSize: 0,
    InitGameSpace: function () {
        //read local setting file

    },
    IsEmpty: function (x, y) {

    },
    Redraw: function () {
    },
    StopSquare: function (square, x, y) {
    },
    CheckLines: function () {
    }
}

Utity = {
    BrowserType: function () {
        var OsObject = "";
        if (navigator.userAgent.indexOf(BrowserTypes.MSIE) > 0) {
            return BrowserTypes.MSIE;
        }
        if (isFirefox = navigator.userAgent.indexOf(BrowserTypes.Firefox) > 0) {
            return BrowserTypes.Firefox;
        }
        if (isSafari = navigator.userAgent.indexOf(BrowserTypes.Safari) > 0) {
            return BrowserTypes.Safari;
        }
        if (isCamino = navigator.userAgent.indexOf(BrowserTypes.Camino) > 0) {
            return BrowserTypes.Camino;
        }
        if (isMozilla = navigator.userAgent.indexOf(BrowserTypes.Gecko) > 0) {
            return BrowserTypes.Gecko;
        }
    },
    AddEvent: function (oTarget, sEventType, funName) {
        if (oTarget.addEventListener) {//for DOM;
            oTarget.addEventListener(sEventType, funName, false);
        }else if (oTarget.attachEvent) {
            oTarget.attachEvent("on" + sEventType, funName);
        }else {
            oTarget["on" + sEventType] = funName;
        }
    },
    RemoveEvent: function (oTarget, sEventType, funName) {
        if (oTarget.removeEventListener) {//for DOM;
            oTarget.removeEventListener(sEventType, funName, false);
        }else if (oTarget.detachEvent) {
            oTarget.detachEvent("on" + sEventType, funName);
        }else {
            oTarget["on" + sEventType] = null;
        }
    },
    ReadFile: function read(path) {
        var content = "";
        try {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var reader = fso.openTextFile(path, 1);
            while (!reader.AtEndofStream) {
                content += reader.readline();
                content += "\n";
            }
            // close the reader
            reader.close();
        } catch (e) {
            if (document.implementation && document.implementation.createDocument) {
                try {
                    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
                    var lf = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
                    lf.initWithPath(path);
                    if (lf.exists() == false) {
                        alert("File does not exist");
                    }

                    var fis = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);
                    fis.init(lf, 0x01, 00004, null);
                    var sis = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
                    sis.init(fis);
                    var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
                    content = converter.ConvertToUnicode(sis.read(sis.available()));
                    converter.charset = "UTF-8";
                }
                catch (e) {
                    alert("Mozilla Firefox read local file error: \n" + e);
                }
            }
        }
        return content;
    },
    WriteFile: function (arrstr, path) {
        try {
            var fso = new ActiveXObject("Scripting.FileSystemObject");
            var fh = fso.CreateTextFile(path, true);
            fh.WriteLine(arrstr);
            fh.Close();
        } catch (e) {
            try {
                var pm = netscape.security.PrivilegeManager;
                pm.enablePrivilege('UniversalXPConnect');
            } catch (e) {
                alert(" Enter 'about:config' Select 'signed.applets.codebase_principal_support' Change to'true'");
            }
            var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);

            // alert("create file object");
            if (file instanceof Components.interfaces.nsILocalFile)
                file.initWithPath(path);
            else {
                alert("erro");
                return;
            }
            //alert("init file object");
            var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
            //alert("create fout object");

            try {
                outputStream.init(file, 0x04 | 0x08 | 0x20, 420, 0);
            } catch (e){ alert(e); }
            try {
                for (var i = 0; i < arrstr.length; i++) {
                    arrstr[i] += "\r\n";
                    outputStream.write(arrstr[i], arrstr[i].length);
                }
            } catch (e){ alert(e); };
            outputStream.close();
        }
    }
}

Block = {
    Blocktype: BlockType.undefined,
    Location: null,
    BackColor: null,
    FoceColor: null,
    MyRotation: RotateDirections.South,
    Squares1: null,
    Squares2: null,
    Squares3: null,
    Squares4: null,
    SquareSize: null,
    InitBlock: function (location, type) {
        var _iSeed = Math.floor(Math.random() * GameSpace.BlockColor.Length);
        this.BackColor = GameSpace.BlockColor[_iSeed];
        this.ForeColor = GameSpace.BlockColor[_iSeed];

        if (type == BlockType.undefined) {
            var _num = Math.floor(Math.random() * 8);
            this.Blocktype = _num;
        }
        else
            this.Blocktype = type;

        this.Location = location;
        //this.SquareSize = new Size(GameSpace.SquareSize, GameSpace.SquareSize);
        this.New4Square(this.Blocktype);
    },
    Draw: function (Handle) {
    },
    Erase: function (Handle) {
    },
    New4Square: function (Blocktype) {
        Squares1 = new Square.InitSquare(this.SquareSize, this.ForeColor, this.BackColor);
        Squares2 = new Square.InitSquare(this.SquareSize, this.ForeColor, this.BackColor);
        Squares3 = new Square.InitSquare(this.SquareSize, this.ForeColor, this.BackColor);
        Squares4 = new Square.InitSquare(this.SquareSize, this.ForeColor, this.BackColor);
    },
    Down: function () {
    },
    Left: function () {
    },
    Right: function () {
    },
    Rotate: function () {
    },
    Top: function () {
    }
}

Square = {
    Location: null,
    size: null,
    BackColor: null,
    FoceColor: null,
    InitSquare: function (initSize, initForeColor, initBackColor) {
        this.size = initSize;
        this.ForeColor = initForeColor;
        this.BackColor = initBackColor;
        return this;
    },
    Draw: function (Handle) {
    },
    Erase: function (Handle) {
    }
}

BlockType = {
    undefined: 0,
    I: 1,
    L1: 2,
    L2: 3,
    Z1: 4,
    Z2: 5,
    T: 6,
    O: 7
}
RotateDirections = {
    North: 1,
    East: 2,
    South: 3,
    West: 4
}
Speeds = {
    slower: 1000,
    slow: 800,
    quick: 500,
    quicker: 350,
    quickest: 250
}
BrowserTypes = {
    MSIE: "MSIE",
    Firefox: "Firefox",
    Safari: "Safari",
    Camino: "Camino",
    Gecko: "Gecko"
}
